define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/index',
                    add_url: 'merchant/add',
                    edit_url: 'merchant/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'merchant/multi',
                    table: 'merchant',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'birthday', title: __('Birthday'), operate: false},
                        {field: 'address', title: __('Address'), operate: false},
                        {field: 'qrcode', title: __('Qrcode'), operate: false, formatter: Table.api.formatter.image},
                        {field: 'shop_end_time', title: '商家身份到期日期', operate:'RANGE', addclass:'datetimerange'},
                        {field: 'published_activity_num', title: "已发布活动数", operate: false, formatter: function (value, row, index) {
                                url = "activity?user_id=" + row.id;
                                this.url = url;
                                return Table.api.formatter.addtabs.call(this, value, row, index);
                            }},
                        {field: 'order_num', title: "累计订单数", operate: false, formatter: function (value, row, index) {
                                url = "activity.order?shop_id=" + row.id;
                                this.url = url;
                                return Table.api.formatter.addtabs.call(this, value, row, index);
                            }},
                        {field: 'all_withdraw_money', title: '总金额', operate: 'between'},
                        {field: 'done_withdraw_money', title: '已提现金额', operate: 'between'},
                        {field: 'withdraw_record_num', title: "提现记录", operate: false, formatter: function (value, row, index) {
                                url = "withdraw/request?user_id=" + row.id;
                                this.url = url;
                                return Table.api.formatter.addtabs.call(this, value, row, index);
                            }},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'pay_button',
                                    text: '商家交费历史',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-check-square',
                                    url: function (row) {
                                        return Fast.api.fixurl('merchant/payhistory?user_id=' + row.id)
                                    },
                                    extend: "data-area='[\"60%\", \"60%\"]'",
                                },
                                {
                                    name: 'withdraw_history_button',
                                    text: '提现方式',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-check-square',
                                    url: function (row) {
                                        return Fast.api.fixurl('merchant/setwithdrawway?user_id=' + row.id)
                                    },
                                    extend: "data-area='[\"60%\", \"60%\"]'",
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        setwithdrawway: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
